package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import domain.Inventory;
public class InventoryDao extends BaseDao{
	@SuppressWarnings("rawtypes")
	public ArrayList select(String sql)throws Exception{
		ArrayList<Inventory>result =new ArrayList<Inventory>();
		Connection conn =null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		conn=getConnection();
		pstmt=conn.prepareStatement(sql);
		rs=pstmt.executeQuery();
		while(rs.next()){
			Inventory obj=new Inventory();
			obj.setItemid(rs.getString("itemid"));
			obj.setQty(rs.getString("qty"));
			result.add(obj);
		}
		rs.close();
		pstmt.close();
		conn.close();
		return result;	
	}
	public void insert(Inventory obj)throws Exception{
		Connection conn =null;
		PreparedStatement ps=null;
		conn=getConnection();
		String sql="insert into inventory values(?,?)";
		ps=conn.prepareStatement(sql);
		ps.setString(1, obj.getItemid());
		ps.setString(2, obj.getQty());
		ps.executeUpdate();
		ps.close();
		conn.close();
	}
	public void update(Inventory obj)throws Exception{
		Connection conn =null;
		PreparedStatement ps=null;
		conn=getConnection();
		String sql="update inventory set qty=? where itemid=?";
		ps=conn.prepareStatement(sql);
		ps.setString(1, obj.getQty());
		ps.setString(2, obj.getItemid());
		ps.executeUpdate();
		ps.close();
		conn.close();
	}
	public void delete(Inventory obj)throws Exception{
		Connection conn =null;
		PreparedStatement ps=null;
		conn=getConnection();
		String sql="delete from inventory where itemid=?";
		ps=conn.prepareStatement(sql);
		ps.setString(1, obj.getItemid());
		ps.executeUpdate();
		ps.close();
		conn.close();
	}
	@SuppressWarnings("rawtypes")
	public Inventory getInventory(String inventoryId) throws Exception{
		Inventory obj=null;
		ArrayList list=select("select*from inventory where itemid='"+inventoryId+"'");
		if(list.size()>0){
			obj=(Inventory) list.get(0);
		}
		return obj;
	}

}
